查看原文
其他

ansible自动化:LAMP环境部署

木讷大叔爱运维 木讷大叔爱运维 2022-07-13

点击上方蓝色字体,关注我们




读完需 4 分钟

速读需 2 分钟 



Playbooks 是 Ansible的配置、部署、编排语言。他们可以被描述为一个需要希望远程主机执行命令的方案,或者一组IT程序运行的命令集合。

言简意赅,Playbooks就是我们现阶段操作自动化的解决方案。通过Playbooks的编排,我们可以按需定制各种自动化操作。


本次我们介绍LAMP环境自动化部署,将其拆分为以下几个阶段:

  • 批量编译安装lamp(apache+php及扩展);

  • 初始化配置(自动加载php模块,修改mpm、日志轮储等);

  • 手动按需修改站点文件;


1

配置思路


ansible-playbook配置思路,如下:

  1. 通过vars中的main.yml配置变量,主要为源码存放目录及安装目录;

  2. 通过tasks中的copy.yml将源码文件传输到异地服务器上源码存放目录;

  3. 通过tasks中的install.yml调用模板lamp_install.sh,将lamp安装到变量中定义的安装目录;

  4. 通过tasks中的main.yml调用copy模块和install模块;

  5. 通过lamp.yml调用剧本(playbook):lamp_install实现自动化部署;


2

目录结构


我们按playbook组织目录结构,其中:

  • files:存放需要同步到异地服务器的源码文件及配置文件;

  • handlers:当资源发生变化时需要进行的操作,若没有此目录可以不建或为空;

  • meta:角色定义可留空;

  • tasks:lamp安装过程成需要进行的执行的任务;

  • templates:用于执行lamp安装的模板文件,一般为脚本;

  • vars:本次安装定义的变量


[root@test ansible]# cd /etc/ansible/[root@test ansible]# mkdir -p roles/lamp_install/{files,handlers,meta,tasks,templates,vars}[root@test ansible]# tree /etc/ansible├── ansible.cfg├── hosts├── lamp.yml├── log│ └── ansible.log├── roles│ ├── lamp_install│ │ ├── files│ │ │ ├── apache│ │ │ │ ├── apr-1.5.0.tar.gz│ │ │ │ ├── apr-util-1.5.3.tar.gz│ │ │ │ ├── cHost.conf│ │ │ │ ├── httpd-2.4.7.tar.gz│ │ │ │ ├── httpd-mpm.conf│ │ │ │ ├── pcre-8.36.tar.gz│ │ │ │ ├── rewrite.conf│ │ │ │ └── symfony.zip│ │ │ ├── memcached│ │ │ │ ├── libevent-2.0.22-stable.tar.gz│ │ │ │ ├── magent-0.5.tar.gz│ │ │ │ └── memcached-1.4.22.tar.gz│ │ │ └── php│ │ │ ├── eaccelerator-eaccelerator-42067ac.tar.gz│ │ │ ├── libmcrypt-2.5.7-1.2.el6.rf.x86_64.rpm│ │ │ ├── libmcrypt-devel-2.5.7-1.2.el6.rf.x86_64.rpm│ │ │ ├── memcached_extension│ │ │ │ ├── libmemcached-1.0.18.tar.tar│ │ │ │ └── memcached-2.2.0.tgz│ │ │ ├── memcache_extension│ │ │ │ └── memcache-2.2.7.tgz│ │ │ ├── mongo-1.2.10.tgz│ │ │ └── php-5.4.22.tar.gz│ │ ├── handlers│ │ ├── meta│ │ ├── tasks│ │ │ ├── copy.yml│ │ │ ├── install.yml│ │ │ └── main.yml│ │ ├── templates│ │ │ └── lamp_install.sh│ │ └── vars│ │ └── main.yml


3

具体实现


1.创建lamp角色文件,用于调用lamp_install

[root@test ansible]# vim lamp.yml- hosts: test remote_user: root gather_facts: False roles:    - lamp_install

2.创建变量文件

变量文件作用是提取变量,以适应定义化部署需求。

[root@test ansible]# cd /etc/ansible/roles/lamp_install/vars[root@test ansible]#vim main.yml#源码存放目录source_dir: /home/ap/src/lamp/#源码安装目录install_dir: /home/ap/

3.创建任务文件

任务文件的作用是将各种源码同步到远程机器上,做安装前的准备工作。

[root@test ansible]# cd /etc/ansible/roles/mysql_install/tasks[root@test ansible]# vim copy.yml#复制php组件至目标服务器- name: copy php dir to client copy: src=php dest={{source_dir}} owner=root group=root#复制apache组件至目标服务器- name: copy apache dir to client copy: src=apache dest={{source_dir}} owner=root group=root#复制memcached组件至目标服务器- name: copy memcached dir to client copy: src=memcached dest={{source_dir}} owner=root group=root#复制模板文件至目标服务器- name: copy lamp script to client template: src=lamp_install.sh dest={{source_dir}} owner=root group=root mode=0775[root@test ansible]# vim install.yml#执行模板文件进行安装- name: install lamp shell: bash {{source_dir}}/lamp_install.sh[root@test ansible]# vim main.yml#引用copy、install模块- include: copy.yml- include: install.yml

注意:

  • copy模块复制目录,需要加上递归参数recurse;

  • copy模块复制目录,没有目录将会在目标服务器上创建;

  • copy模块复制文件到目标服务器的某一个目录下,需要在dest参数上加上/home/ap/src/lamp/,而不是/home/ap/lamp,否则ansible将会把文件复制为lamp,而不是放在lamp目录下;


4.编写模板脚本

模板脚本的作用是安装部署并进行相关组件的配置,主要如下:

  • 安装apache及相关组件

  • 安装memcached、magent

  • 安装php及相关扩展mogo、memcached、memcache、soap、gd、mbstring、exif、eaccelerator,并添加至php.ini;在apache中引用php

  • 修改http.conf,包括添加rewrite模块,修改AllowOverride等

  • 修改http-mpm.conf

  • 添加rewrite.conf

  • 添加站点配置文件

[root@test ansible]#cd /etc/ansible/roles/lamp_install/templates#!/bin/bash#author:yanggd#comment:lamp环境部署source_dir={{source_dir}}apache=$source_dir/apachephp=$source_dir/phpmemcached=$source_dir/memcachedinstall_dir={{install_dir}}#Source function library.. /etc/init.d/functions#安装apachecd $apachetar -zxvf apr-1.5.0.tar.gzcd apr-1.5.0./configure --prefix=$install_dir/aprmake && make installif [ $? -ne 0 ];then action "install apr is failed!" /bin/false exit $?ficd ..#tar -zxvf apr-util-1.5.3.tar.gzcd apr-util-1.5.3./configure --prefix=$install_dir/apr-util --with-apr=$install_dir/aprmake && make installif [ $? -ne 0 ];then action "install apr-util is failed!" /bin/false exit $?ficd ..#tar -zxvf pcre-8.36.tar.gzcd pcre-8.36./configure --prefix=$install_dir/pcremake && make installif [ $? -ne 0 ];then action "install pcre is failed!" /bin/false exit $?fi#cd ..tar -zxvf httpd-2.4.7.tar.gz cd httpd-2.4.7./configure --prefix=$install_dir/apache --with-apr=$install_dir/apr --with-apr-util=$install_dir/apr-util --with-pcre=$install_dir/pcre --enable-modules=mall --enable-rewrite --enable-mpms-shared=all --with-mpm=event --enable-v4-mapped --enable-somake && make installif [ $? -ne 0 ];then action "install httpd is failed!" /bin/false exit $?ficd ..#unzip symfony.zip -d $install_dircd ..
#安装memcachedcd $memcachedtar -zxvf libevent-2.0.22-stable.tar.gzcd libevent-2.0.22-stable./configure --prefix=$install_dir/libeventmake && make installif [ $? -ne 0 ];then action "install libevent is failed!" /bin/false exit $?ficd ..tar -zxvf memcached-1.4.22.tar.gzcd memcached-1.4.22./configure --prefix=$install_dir/memcached --with-libevent=$install_dir/libeventmake && make installif [ $? -ne 0 ];then action "install memcached is failed!" /bin/false exit $?fi#安装magentcd ..ln -s $install_dir/libevent/lib/libevent-2.0.so.5 /lib64/libevent-2.0.so.5tar -zxf magent-0.5.tar.gzsed -i "s#LIBS = -levent#LIBS = -levent -lm -L$install_dir/libevent/lib#g" Makefilesed -i "/LIBS/a INCLUDE=-I$install_dir/libevent/include" Makefilesed -i "1i\#ifndef SSIZE_MAX" ketama.hsed -i "4i\#define SSIZE_MAX 32676" ketama.hsed -i '$i\#endif' ketama.h#编译makemkdir -p $install_dir/magent/binmv magent $install_dir/magent/bin/
cd ..#安装phpyum install -y libxml2-devel libjpeg-devel libpng-devel freetype-devel openssl-devel libcurl-devel unzipcd $phprpm -ivh libmcrypt-2.5.7-1.2.el6.rf.x86_64.rpmrpm -ivh libmcrypt-devel-2.5.7-1.2.el6.rf.x86_64.rpmtar -zxvf php-5.4.22.tar.gzcd php-5.4.22./configure --prefix=$install_dir/php --with-apxs2=$install_dir/apache/bin/apxs --with-openssl --with-mcrypt --with-zlib --with-libxml-dir --enable-xml --with-freetype-dir --with-curl --enable-sockets --with-config-file-path=$install_dir/php/etc --with-mysql --with-mysqli --with-pdo-mysqlmake && make installif [ $? -ne 0 ];then action "install php is failed!" /bin/false exit $?ficp php.ini-production $install_dir/php/etc/php.inised -i '/mime.types/a\ AddType application/x-httpd-php .php' $install_dir/apache/conf/httpd.confsed -i '/mime.types/a\ AddType application/x-httpd-php-source .phps' $install_dir/apache/conf/httpd.confsed -i '/;date.timezone/a date.timezone = "Asia/Shanghai"' $install_dir/php/etc/php.inised -i '/;date.timezone/a date.timezone = PRC' $install_dir/php/etc/php.ini
cd ../#安装php扩展#安装mono扩展tar -zxvf mongo-1.2.10.tgzcd mongo-1.2.10$install_dir/php/bin/phpize./configure --with-php-config=$install_dir/php/bin/php-config make && make installsed -i '/php_bz2.dll/a extension=mongo.so' $install_dir/php/etc/php.inicd ..#安装memcached扩展cd memcached_extensiontar -zxvf libmemcached-1.0.18.tar.tarcd libmemcached-1.0.18./configure --prefix=$install_dir/libmemcached --with-memcachedmake && make installcd ..tar -zxvf memcached-2.2.0.tgzcd memcached-2.2.0$install_dir/php/bin/phpize./configure --enable-memcached --with-php-config=$install_dir/php/bin/php-config --with-libmemcached-dir=$install_dir/libmemcached --disable-memcached-saslmake && make installsed -i '/php_bz2.dll/a extension=memcached.so' $install_dir/php/etc/php.inicd ../../#安装memcache扩展cd memcache_extensiontar -zxvf memcache-2.2.7.tgzcd memcache-2.2.7$install_dir/php/bin/phpize./configure --with-php-config=$install_dir/php/bin/php-config --enable-memcachemake && make installsed -i '/php_bz2.dll/a extension=memcache.so' $install_dir/php/etc/php.inicd ../../#安装soapcd php-5.4.22/ext/soap/$install_dir/php/bin/phpize./configure --enable-soap --with-php-config=$install_dir/php/bin/php-configmake && make installsed -i '/php_bz2.dll/a extension=soap.so' $install_dir/php/etc/php.inicd ../#安装gd扩展cd gd$install_dir/php/bin/phpize./configure --with-png-dir --with-freetype-dir --with-jpeg-dir --with-gd --with-php-config=$install_dir/php/bin/php-configmake && make installsed -i '/php_bz2.dll/a extension=gd.so' $install_dir/php/etc/php.inicd ..#安装mbstring扩展cd mbstring$install_dir/php/bin/phpize./configure --with-php-config=$install_dir/php/bin/php-configmake && make installsed -i '/php_bz2.dll/a extension=mbstring.so' $install_dir/php/etc/php.inicd ..#安装exif扩展cd exif$install_dir/php/bin/phpize./configure --with-php-config=$install_dir/php/bin/php-configmake && make installsed -i '/php_bz2.dll/a extension=exif.so' $install_dir/php/etc/php.inicd ../../../#安装eaccelerator扩展tar -zxvf eaccelerator-eaccelerator-42067ac.tar.gzcd eaccelerator-eaccelerator-42067ac$install_dir/php/bin/phpize./configure --enable-eaccelerator=shared --with-php-config=$install_dir/php/bin/php-configmake && make installsed -i '/php_bz2.dll/a extension=eaccelerator.so' $install_dir/php/etc/php.inimkdir -p $install_dir/eaccelerator_cachechmod -R 777 $install_dir/eaccelerator_cachecat >> $install_dir/php/etc/php.ini <<EOFeaccelerator.shm_size="64"eaccelerator.cache_dir="$install_dir/eaccelerator_cache"eaccelerator.enable="1"eaccelerator.optimizer="1"eaccelerator.check_mtime="1"eaccelerator.debug="0"eaccelerator.filter="!*.yml.php"eaccelerator.shm_max="0"eaccelerator.shm_ttl="3600"eaccelerator.shm_prune_period="3600"eaccelerator.shm_only="0"eaccelerator.compress="1"eaccelerator.compress_level="9"eaccelerator.allowed_admin_path = "/web/smallcity.cityhouse.cn"EOF
##prepare apache#修改 http.confsed -i "s/#LoadModule rewrite_module/LoadModule rewrite_module/g" $install_dir/apache/conf/httpd.confsed -i "s/#ServerName www.example.com:80/ServerName localhost:80/g" $install_dir/apache/conf/httpd.confsed -i "s/AllowOverride None/AllowOverride All/g" $install_dir/apache/conf/httpd.confsed -i "s/AllowOverride none/AllowOverride All/g" $install_dir/apache/conf/httpd.confzsed -i "s/Require all denied/Require all granted/g" $install_dir/apache/conf/httpd.confsed -i "s#logs/access_log#| $install_dir/apache/bin/rotatelogs $install_dir/apache/logs/access_log.%Y-%m-%d 86400 480#g" $install_dir/apache/conf/httpd.conf#prepare *.conf\cp $apache/http-mpm.conf $install_dir/apache/conf/extra/\cp $apache/rewrite.conf $install_dir/apache/conf/\cp $apache/cHost.conf $install_dir/apache/conf/
#开机启动echo "$install_dir/apache/bin/apachectl -k start" >> /etc/rc.local

5.部署

#检查文件[root@test ansible]# ansible-playbook -C lamp.yml#执行playbook[root@test ansible]# ansible-playbook lamp.yml



K8SEASY:一键安装K8S高可用集群

运维思索:基础设施自动化落地

运维思索:运维规范如何生成?

一套包含完整前后端的系统如何在K8S中部署?

滴滴夜莺:从监控告警系统向运维平台演化

蓝鲸实现vsphere虚拟机交付 -虚拟机管理(VSPHERE)



你与世界

只差一个

公众号




您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存